//
//  HICEmoticonKeyboard.swift
//  vibo
//
//  Created by jht-seven on 16/7/4.
//  Copyright © 2016年 jht-seven. All rights reserved.
//

import UIKit

class HICEmoticonKeyboard: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        // 添加控件
        addSubview(toolbar)
        addSubview(pageView)
        
        // 添加约束
        toolbar.snp_makeConstraints { (make) in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        
        pageView.snp_makeConstraints { (make) in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(toolbar.snp_top)
        }
        
        //接受toobar中的哪个按钮点击
        toolbar.toolbarClosure = {[weak self] (type: HICEmoticonToolbarType) ->() in
            //获取indexPath
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            //叫配图滚动指定位置
            self?.pageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        }
        
        
    }
    
    // MARK: - 懒加载控件
    // 如果使用UIStackView初始化 需要传入frame如果我们使用约束frame可以传入CGRectZero
    private lazy var toolbar: HICEmoticonToolbar = HICEmoticonToolbar(frame: CGRectZero)
    
    //表情键盘
    private lazy var pageView: HICEmoticonPageView = {
        
        let view = HICEmoticonPageView()
        
        //设置代理
        view.delegate = self
        //取消弹簧效果
        view.bounces = false
        //分页
        view.pagingEnabled = true
        //隐藏滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        
        return view
    }()
    
}

//UICollectionViewDelegate
extension HICEmoticonKeyboard: UICollectionViewDelegate{

    func scrollViewDidScroll(scrollView: UIScrollView) {
        //获取相对于屏幕的中心点
        let centerX = SCREENW / 2 + scrollView.contentOffset.x
        
        //获取当前屏幕显示的cell 按照我们的意愿重新排序
        let cellArray = pageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        // 如果屏幕显示的是两个cell时候我们才判断
        if cellArray.count == 2 {
            
            //第一个cell
            let firstCell = cellArray.first!
            //第二个cell
            let lastCell = cellArray.last!
            
            //获取显示多的cell的indexPath
            var indexPath: NSIndexPath
            
            //判断我们的额中心点是否在第一个cell中 代表第一个cell显示的多
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                indexPath = pageView.indexPathForCell(firstCell)!
            }else{
                //最后一个显示的多
                indexPath = pageView.indexPathForCell(lastCell)!
            }
            
            print(indexPath.section)
            //滚动pageView 改变toobar下面按钮的选中的状态
            toolbar.setCurrnetButtons(indexPath.section)
        }
    }
}
